/*var root = /!*[[${root}]]*!/0;
var seconds = /!*[[${map.seconds}]]*!/0;
var sheets = /!*[[${map.sheet}]]*!/null;
var remaining = /!*[[${map.remaining}]]*!/3600;
var expire = /!*[[${map.expire}]]*!/0;*/
var updateSheetUrl = '';
var submitUrl = '';
var scoreDetail = '';
console.log(root);
if (root != null && root != '') {
    updateSheetUrl = root + '/api/v1/simulation/updateSheet';
    submitUrl = root + '/api/v1/simulation/submit';
    scoreDetail = root + '/redirect/center/simulation/detail';
}
var finalScore = 0;
layui.use('form', function () {
    var form = layui.form;

    function loadNewQuestions() {
        let sheetHtml = '';
        let firstUnFinish = true;
        for (var i = 0; i < sheets.length; i++) {
            let sheet = sheets[i];
            if (sheet.answer != null && sheet.answer != undefined) {
                var split = sheet.answer.split(",");
                $.each(split, function (ind, el) {
                    let _this = $('#' + sheet.questionId).find('.layui-form input[answer-id=' + el + ']');
                    let type = _this.attr('type');
                    _this.attr('checked', true);
                    form.render();
                })
            }
            sheetHtml += '<span order="' + sheet.order + '" answer="' + sheet.answer + '" que-id="' + sheet.questionId + '"';
            if (sheet.status == 0) {
                if (firstUnFinish) {
                    firstUnFinish = false;
                    sheetHtml += 'class="curr" ';
                    $('.contLeft').find('.examBox[exam-index=1]').hide();
                    $('.contLeft').find('.examBox[exam-index=' + (i + 1) + ']').show();
                }
            } else {
                sheetHtml += 'class="chose" ';
            }
            sheetHtml += ' >';
            if (sheet.mark == 1) {
                sheetHtml += '<span class="layui-icon layui-icon-flag"></span>';
            }
            sheetHtml += (i + 1) + '</span>';
        }
        $('#sheet').html(sheetHtml);
    }

    loadNewQuestions();
    $(function () {
        var time = lay_tz_ui.tools.secondsFormat(seconds);
        $('#counter_2').countdown({
            image: root + '/jcountdown/img/digits.png',
            startTime: time,
            timerEnd: function () {
                //自动提交 试卷
                submit();
            },
            format: 'hh:mm:ss'
        });
        setInterval(function () {
            remaining--;
        }, 1000);
    });

    //点击答题卡
    $('#sheet').on('click', 'span', function () {
        let index = parseInt($(this).text());

        $(this).addClass('curr');
        $(this).siblings().each(function () {
            $(this).removeClass('curr')
        });
        sheetClick(index);
    });

    function sheetClick(index) {
        $('.contLeft').find('.examBox').hide();
        $('.contLeft').find('.examBox[exam-index=' + index + ']').show();
    }

    //上一题
    $('.prevBtn').on('click', function () {
        prevExam();
    });
    //下一题
    $('.nextBtn').on('click', function () {
        nextExam();
    });

    function nextExam() {
        let _this = $('.curr');
        let cardIndex = _this.text();
        let nextSpan = _this.next('span').text();
        if (nextSpan == '') {
            lay_tz_ui.itWin.msg("已经是最后一道题了！", 7);
            return false;
        }
        _this.next('span').addClass('curr');
        _this.removeClass('curr');
        let nextIndex = parseInt(cardIndex) + 1;
        $('.contLeft').find('.examBox[exam-index=' + cardIndex + ']').hide();
        $('.contLeft').find('.examBox[exam-index=' + nextIndex + ']').show();
    }

    function prevExam() {
        let _this = $('.curr');
        let cardIndex = _this.text();
        let nextSpan = _this.prev('span').text();
        if (nextSpan == '') {
            lay_tz_ui.itWin.msg("已经是第一道题了！", 7);
            return false;
        }
        _this.prev('span').addClass('curr');
        _this.removeClass('curr');
        let nextIndex = parseInt(cardIndex) - 1;
        $('.contLeft').find('.examBox[exam-index=' + cardIndex + ']').hide();
        $('.contLeft').find('.examBox[exam-index=' + nextIndex + ']').show();
    }

    $('.confirm').on('click', function () {
        let type = $(this).attr('sty');
        type = type == 'radio' ? 'radio' : 'check';
        let answer = '';
        $.each($(this).siblings('div'), function (ind, el) {
            let answerId = $(el).find('.layui-form-' + type + 'ed').prev('input').attr('answer-id');
            if (answerId != undefined) {
                answer = answer == '' ? answerId : answer + ',' + answerId;
            }
        });
        if (type == 'radio') {
            if (answer == '') {
                lay_tz_ui.itWin.msg("单选题，请选择你认为正确的答案！", 7);
                return false;
            }
        } else {
            if (answer.split(",").length < 2) {
                lay_tz_ui.itWin.msg("多选题，请至少选择两个正确答案！", 7);
                return false;
            }
        }
        let order = $('.curr').attr('order');
        let questionId = $('.curr').attr('que-id');
        let mark = $('.curr').find('.layui-icon-flag').length;
        $.post(updateSheetUrl, {
            "answer": answer,
            "order": order,
            "status": 1,
            "mark": mark,
            "questionId": questionId
        }, function (res) {
            $('.curr').attr('answer', answer);
            $('.curr').addClass('chose');
            nextExam();
        })

    });

    //交卷
    $('.papers').on('click', function () {
        if (remaining > 0) {
            lay_tz_ui.itWin.msg("开考" + expire + "分钟内不得交卷！", 7);
            return false;
        } else {
            let spans = $('#sheet').find('>span');
            let choseSpan = $('#sheet').find('.chose');
            lay_tz_ui.itWin.confirm('试题总数【' + spans.length + '】道，当前完成【' + choseSpan.length + '】道，确定交卷？', function () {
                submit();
            })
        }
    });

    function submit() {
        let answers = [];
        let spans = $('#sheet').find('span');
        $.each(spans, function (ind, el) {
            let answer = $(el).attr('answer');
            let questionId = $(el).attr('que-id');
            let status = $(el).hasClass('chose') ? 1 : 0;
            answers.push({"answer": answer, "questionId": questionId, "status": status});
        });
        $.post(submitUrl, {answers: JSON.stringify(answers)}, function (res) {
            if (res.code == 0) {
                finalScore = res.data;
                parent.lay_tz_ui.itWin.open({
                    title: "模拟考试试卷详情",
                    area: ['100%', '100%'],
                    content: [scoreDetail + '/' + finalScore, 'no']
                });
                parent.lay_tz_ui.itWin.close(parent.layer.getFrameIndex(window.name))
            }
        })
    }

    $('#sheet').on('click', 'span span', function () {
        $(this).remove();
    });

    //标记
    $('.tipBtn').on('click', function () {
        let examIndex = parseInt($(this).parents('.examBox').attr('exam-index')) - 1;
        let choosed = $('#sheet').find('>span').eq(examIndex);
        if (choosed.find('img').length == 0) {
            choosed.append(' <img src="'+root+'/common/images/tip.png">');
        } else {
            choosed.find('img').remove();
        }
    });

});

function showScore() {
    parent.lay_tz_ui.itWin.open({
        title: "模拟考试结果",
        area: ['100%', '100%'],
        content: [showScore + '/' + finalScore, 'no']
    });
}

function retry(id, isNew) {
    parent.openSimulation(id, isNew);
}

